package com.voiceblog.model;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.voiceblog.dao.IBlogDAO;

/**
 * A data access object (DAO) providing persistence and search support for Blog
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.voiceblog.model.Blog
 * @author MyEclipse Persistence Tools
 */

public class BlogDAO extends HibernateDaoSupport implements IBlogDAO {
	private static final Log log = LogFactory.getLog(BlogDAO.class);

	protected void initDao() {
		// do nothing
	}

	public List findTopBlogs(final int num) {

		final String queryString = "from Blog as model  order by model.visitNumber desc ";
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				Query query = session.createQuery(queryString);
				query.setFirstResult(0);
				query.setMaxResults(num);
				List list = query.list();
				return list;
			}
		});
		return list;
	}

	public List findRecentlyBlogs(String name) {
		log.debug("finding Users instance by example");
		try {
			String queryString = "from Blog as model where model.users.account=? order by model.articlePublishDate desc ";
			return getHibernateTemplate().find(queryString, name);
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findPreviousBlogs(String name, Blog blog) {
		log.debug("finding Users instance by example");
		try {
			String queryString = "from Blog as model where model.users.account=? and model.articleId < "
					+ blog.getArticleId()
					+ "order by model.articlePublishDate desc";
			return getHibernateTemplate().find(queryString, name);
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByHql(final String hql) {
		log.debug("getting Blog instance with hql: " + hql);
		try {
			return getHibernateTemplate().executeFind(new HibernateCallback() {
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query query = session.createQuery(hql);
					List list = query.list();
					session.close();
					return list;
				}
			});

		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findBlogByTitle(String title) {
		log.debug("finding Users instance by example");
		try {
			String queryString = "from Blog as model where model.articleTitle like ?";
			return getHibernateTemplate().find(queryString, title);
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findBlogByDate(Date date) {
		log.debug("finding Users instance by example");
		try {
			String queryString = "from Blog as model where model.articlePublishDate =?";
			return getHibernateTemplate().find(queryString, date);
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findNextBlogs(String name, Blog blog) {
		log.debug("finding Users instance by example");
		try {
			String queryString = "from Blog as model where model.users.account=? and model.articleId > "
					+ blog.getArticleId();
			return getHibernateTemplate().find(queryString, name);
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public void updateBlog(Blog blog) {
		getHibernateTemplate().update(blog);
	}

	public List findRecentlyBlogs(final int num, final Users user) {

		final String queryString = "from Blog as model where model.users.account =? order by model.articlePublishDate desc ";
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				Query query = session.createQuery(queryString);
				query.setFirstResult(0);
				query.setMaxResults(num);
				query.setString(0, user.getAccount());
				List list = query.list();
				return list;
			}
		});
		return list;
	}

	public void save(Blog transientInstance) {
		log.debug("saving Blog instance");

		try {

			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void deleteBlog(Blog persistentInstance) {
		log.debug("deleting Blog instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;

		}
	}

	public Blog findById(int id) {

		log.debug("getting Blog instance with id: " + id);
		try {
			Blog instance = (Blog) getHibernateTemplate().get(
					"com.voiceblog.model.Blog", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;

		}
	}

	public List findByExample(Blog instance) {
		log.debug("finding Blog instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Blog instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Blog as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all Blog instances");
		try {
			String queryString = " from Blog as model order by model.visitNumber desc";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Blog merge(Blog detachedInstance) {
		log.debug("merging Blog instance");
		try {
			Blog result = (Blog) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Blog instance) {
		log.debug("attaching dirty Blog instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Blog instance) {
		log.debug("attaching clean Blog instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static BlogDAO getFromApplicationContext(ApplicationContext ctx) {
		return (BlogDAO) ctx.getBean("BlogDAO");
	}

	public void editBlog(Blog blog) {
		attachDirty(blog);
		
	}

}